package client.adminGUI.login;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import client.adminGUI.login.LoginPanel;
import client.adminGUI.GUIAdmin;
import client.backend.Connection;

public class LoginPanel extends JPanel implements ActionListener {
	JButton login;
    JLabel uNameLabel, pWordLabel;
    JPasswordField password;
    JTextField username;
    GUIAdmin parent;
    JLabel messageLabel;
    private static LoginPanel instance = null;
    
    final int LoginX = 450;
    final int LoginY = 300;
    final int LoginW = 128;
    final int LoginH = 30;
    
    final int QuitX = LoginX;
    final int QuitY = 768 - 144;
    final int QuitW = LoginW;
    final int QuitH = LoginH;
    
    final int UsernameX = 356+96;
    final int UsernameY = LoginY-70;
    final int UsernameW = 96;
    final int UsernameH = LoginH;
    
    final int PasswordX = UsernameX;
    final int PasswordY = LoginY-35;
    final int PasswordW = UsernameW;
    final int PasswordH = LoginH;
    
    final int uLabelX = 356;
    final int uLabelY = LoginY-70;
    final int uLabelW = 96;
    final int uLabelH = LoginH;;
    
    final int pLabelX = uLabelX;
    final int pLabelY = uLabelY+35;
    final int pLabelW = uLabelW;
    final int pLabelH = LoginH;
    
    final int MessageX = uLabelX+75;
    final int MessageY = pLabelY+75;
    final int MessageW = 96;
    final int MessageH = 35;
	
    public LoginPanel(){
    	super();
        this.setBounds(0,0,GUIAdmin.WIDTH,GUIAdmin.HEIGHT-50);
        this.setLayout(null);
        this.setBackground(GUIAdmin.Background_Color);
    	login = new JButton("LOGIN");
        uNameLabel = new JLabel("Username");
        pWordLabel = new JLabel("Password");
        password = new JPasswordField();
        username = new JTextField();
        messageLabel = new JLabel();
        
        login.setBounds(LoginX, LoginY, LoginW, LoginH);
        uNameLabel.setBounds(uLabelX, uLabelY, uLabelW, uLabelH);
        pWordLabel.setBounds(pLabelX, pLabelY, pLabelW, pLabelH);
        username.setBounds(UsernameX, UsernameY, UsernameW, UsernameH);
        password.setBounds(PasswordX, PasswordY, PasswordW, PasswordH);
        messageLabel.setBounds(MessageX, MessageY, MessageW, MessageH);
        
        uNameLabel.setForeground(GUIAdmin.Text_Color);
        uNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        pWordLabel.setForeground(GUIAdmin.Text_Color);
        pWordLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        login.addActionListener(this);
        messageLabel.setVisible(false);
        
        login.setVisible(true);
        
        this.add(login);
        this.add(uNameLabel);
        this.add(pWordLabel);
        this.add(messageLabel);
        this.add(username);
        this.add(password);
    }
    
    /**
     * Display a message
     * @param message New message
     */
    public void messageDisplay(String message){
    	messageLabel.setText(message);
    	messageLabel.setVisible(true);
    }
    
    /**
     * Catch actions
     */
	public void actionPerformed(ActionEvent e){
		GUIAdmin.getInstance().clearMessage();
    	if (e.getSource() == login){
    		messageLabel.setVisible(false);
    		if(Connection.getInstance().adminLogin(username.getText(),new String(password.getPassword()))){
    			GUIAdmin.getInstance().loggedIn();
    		}
    		else {
    			messageDisplay("Bad Login");
    		}
    	}
    }
	
	/**
	 * Getter for LoginPanel
	 * @return	Only instance of LoginPanel
	 */
	public static LoginPanel getInstance() {
    	if (instance == null)
    		instance = new LoginPanel();
    	
    	return instance;
    }
}
